package com.yoda.youdianda.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.youdianda.common.R;
import com.yoda.youdianda.domain.YddUser;
import com.yoda.youdianda.domain.YddUser;
import com.yoda.youdianda.domain.YddUserData;
import com.yoda.youdianda.dto.YddArticleDto;
import com.yoda.youdianda.service.UserService;
import com.yoda.youdianda.vo.ArticleVo;
import com.yoda.youdianda.vo.PageResult;
import com.yoda.youdianda.vo.UserLoginFrontVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


//前台用户相关
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;


    //新增文章
    @PostMapping("/addArticle")
    public R<String> save(@RequestBody YddArticleDto yddArticleDto) {
        return userService.save(yddArticleDto);
    }


    //删除文章
    @GetMapping("/delArticle")
    public R<String> del(@RequestParam int id) {
        return userService.del(id);
    }

    //删除文章
    @PostMapping("/editArticle")
    public R<String> update(@RequestBody YddArticleDto yddArticleDto) {
        return userService.update(yddArticleDto);
    }

    //获取用户信息
    @GetMapping("/getUserInfo")
    public R getUserInfo() {
        UserLoginFrontVo userLoginFrontVo = userService.getUserInfo();
        return R.success(userLoginFrontVo);
    }

    //修改用户信息
    @PostMapping("/update")
    public R updateUserInfo(@RequestBody YddUser yddUser) {
        userService.updateUserInfo(yddUser);
        return R.success("修改成功");
    }

    //点赞/取消点赞和收藏/取消收藏
    @GetMapping("userDataHandle")
    public R userDataHandle(Integer type, @RequestParam("article_id") Integer articleId, String action) {
        if ("add".equals(action)) {
            userService.addYddUserData(articleId, type);
        } else if ("del".equals(action)) {
            userService.delYddUserData(articleId, type);
        }
        return R.success("执行成功");
    }

    /**
     * 个人文章分页查询
     *
     * @param page  页面
     * @param limit 限制
     * @return {@link R}
     */
    @GetMapping("/myArticle")
    public R getMyArticles(Integer page, Integer limit) {
        PageResult<ArticleVo> pageResult = userService.selectByPage(page, limit);
        return R.success(pageResult);
    }

    //我的收藏/点赞文章
    @GetMapping("userDataList")
    public R yddUserDataPage(Integer type,Integer page,Integer limit) {
        PageResult pageResult = userService.yddUserDataPage(type,page,limit);
        Map<String,Object> map = new HashMap<>();
        map.put("list",pageResult);
        return R.success(map);
    }
}
